package com.bbs.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.hibernate.validator.util.GetClassLoader;

import com.bbs.comm.SessionContext;
import com.bbs.comm.SysConstant;

public class IsLoginFilter implements Filter{
	Logger logger = Logger.getLogger(IsLoginFilter.class);
	public void destroy() {
		
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession(false);
		try {
			if(session!= null){
				SessionContext context = SessionContext.getSessionContext(session);
				if(context != null){
					if(context.getUser().getUser_id()!=0){//没登陆的都是游客，游客都是0
						chain.doFilter(request, response);
					}else if(context.getUser().getUser_role_id()==SysConstant.SA){
						//是管理员，游客都是0，这样只有在前台登陆的，才能
						res.sendRedirect("../adminLogin.jsp");
					}else{
						res.sendRedirect("../userLogin.jsp");
					}
				}else {
					res.sendRedirect("../userLogin.jsp");
				}
			}else {
				res.sendRedirect("../userLogin.jsp");
			}
		} catch (Exception e) {
			logger.error(e.toString());
		}
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		
	}

}
